package edu.pku.hawk.controller.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtil {

	private static final String PRO_FILE = "hawk.properties";
	private static final String PRO_HOME = "user.home";

	public static final String JMS_SERVER = "JMS.Server";

	private Properties props;

	private static PropertiesUtil instance;

	private PropertiesUtil() {
		props = new Properties();
		try {
			
			String filePath = System.getProperty(PRO_HOME)+ "/" + PRO_FILE;
			
			System.out.println(filePath);
			InputStream in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Enumeration<?> en = props.propertyNames();
		while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
                  String Property = props.getProperty (key);
                  System.out.println(key+" : "+Property);
              }
		System.out.println();

	}

	public static PropertiesUtil getInstance() {
		if (instance == null) {
			instance = new PropertiesUtil();
		}
		return instance;
	}

	// 根据key读取value
	public String readValue(String key) {
		String value = props.getProperty(key);
		System.out.println(key + value);
		return value;
	}

	public static void main(String[] args) {
		String value = PropertiesUtil.getInstance().readValue(JMS_SERVER);
		System.out.println(value);
		System.out.println("OK");
	}
}